﻿using BaseUI;
using BLL;
using System;
using System.Data;
using System.Linq;
using System.Web.UI.WebControls;
using Utility.Helper;

namespace Thksoft.Module.BillManagement
{
    public partial class BillsLedgerReport_Bu : BasePage
    {
        private YX_BILLSLEDGER_BU_BLL blbubll = new YX_BILLSLEDGER_BU_BLL();//blbubll
        protected string filePath = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FORM_BEGIN_DATE.Value = "";

                FORM_OVER_DATE.Value = "";

                ViewState["SortOrder"] = "T2.N_ID";
                ViewState["OrderDire"] = " ASC ";

                BindData(true);
            }
        }

        private void Bind()
        {
            string whereStr = "";
            //出票账号
            if (txtDRAWERACCOUNT.Value.Trim() != "")
            {
                whereStr += " AND T1.C_DRAWERACCOUNT LIKE '%" + txtDRAWERACCOUNT.Value.Trim() + "%'";
            }
            //票据种类
            if (FORM_Type.SelectedValue != "-1")
            {
                whereStr += " AND T1.N_TYPE =" + Convert.ToDecimal(FORM_Type.SelectedValue.ToString());
            }
            //所属公司
            if (FORM_COMPANY_ID.Value != -1)
            {
                whereStr += " AND T2.USER_COMPANY=" + FORM_COMPANY_ID.Value;
            }
            else
            {
                whereStr += @" AND T2.USER_COMPANY in (select cp.cmy_id
  from sys_company cp
 start with cp.cmy_id = " + lognUser.USER_COMPANY + @"
connect by prior cp.cmy_id = cp.cmy_parentid) ";
            }

            //出票银行
            if (txtDRAWERBank.Value != "")
            {
                whereStr += " AND T1.C_DRAWERBANKNAME LIKE '%" + txtDRAWERBank.Value + "%'";
            }

            //票据日期
            if (FORM_BEGIN_DATE.Value.Trim() != "" && FORM_OVER_DATE.Value.Trim() != "")
            {
                whereStr += @" and T1.D_STARTDATE between
       TO_DATE('" + FORM_BEGIN_DATE.Value + @"', 'YYYY-MM-DD') and
       TO_DATE('" + FORM_OVER_DATE.Value + @"', 'YYYY-MM-DD') ";
            }

            ViewState["Where"] = whereStr;

            BindData(true);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSelect_Click(object sender, EventArgs e)
        {
            Bind();
        }

        protected void grvShow1_Sorting(object sender, GridViewSortEventArgs e)
        {
            //点击排序
            string sPage = e.SortExpression;

            if (sPage == "")
            {
                return;
            }
            if (ViewState["SortOrder"].ToString() == sPage)
            {
                if (ViewState["OrderDire"].ToString() == "Desc")
                    ViewState["OrderDire"] = "ASC";
                else
                    ViewState["OrderDire"] = "Desc";
            }
            else
            {
                ViewState["SortOrder"] = e.SortExpression;
                ViewState["OrderDire"] = "ASC";
            }

            BindData(true);
        }

        private void BindData(bool isFirst)
        {
            string Where = "";
            try
            {
                Where = ViewState["Where"].ToString();
            }
            catch
            {
                Where = "AND T2.USER_COMPANY=" + FORM_COMPANY_ID.Value;
            }

            int rowcount = 0;
            if (isFirst)
            {
                PageIng.CurrentPage = 1;
            }
            rowcount = blbubll.GetRecordCount(Where, lognUser.USER_NAME);

            string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];

            DataTable dt = blbubll.GetListByPage(Where, sort, PageIng.CurrentPage, PageIng.RowsPerPage);

            var row = dt.NewRow();
            row["N_TYPE"] = "合计";
            row["N_INVOICEVALUE"] = dt.AsEnumerable().Sum(a => Convert.ToDecimal(a["N_INVOICEVALUE"]));
            dt.Rows.Add(row);

            ViewState["database"] = dt;
            if (dt.Rows.Count > 0)
            {
                PageIng.Visible = true;
                PageIng.RowsCount = rowcount;
                PageIng.DataSource = dt;
                PageIng.GridViewSource = grvShow1;
                PageIng.RowsPerPage = PageIng._rowsPerPage;
                PageIng.Bind();
            }
            else
            {
                PageIng.DataSource = dt;
                PageIng.GridViewSource = grvShow1;
                grvShow1.DataSource = null;
                grvShow1.DataBind();
                PageIng.Visible = false;
            }
            //导出
            BindPint();
        }

        protected void PageIng_DataBinding(object sender, EventArgs e)
        {
            BindData(false);
        }

        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDaoChu_Click(object sender, EventArgs e)
        {
            var BANK = "";
            //出票银行
            if (txtDRAWERBank.Value != "")
            {
                BANK = txtDRAWERBank.Value.ToString();
            }

            var ACCOUNT = "";
            //出票账号
            if (txtDRAWERACCOUNT.Value.Trim() != "")
            {
                ACCOUNT = txtDRAWERACCOUNT.Value.Trim();
            }
            var dt = (DataTable)ViewState["database"];

            #region 导出一

            //dt.Columns.Remove("CMY_NAME");
            //dt.Columns.Remove("C_NAME");
            //dt.Columns.Remove("ACCOUNT");
            //dt.Columns.Remove("C_PAYEEBANKNAME");
            //dt.Columns.Remove("C_PAYEEACCOUNT");
            //dt.Columns.Remove("C_DRAWEENAME");
            //dt.Columns.Remove("REMARK");

            //dt.Columns.Remove("TYPE");
            //dt.Columns.Remove("ID");
            //dt.Columns.Remove("USER_COMPANY");
            //dt.Columns.Remove("DE_BANK_ID");
            //dt.Columns.Remove("N_ID");
            //dt.Columns.Remove("N_TYPE");
            //dt.Columns.Remove("N_DRAWERUSERID");
            //dt.Columns.Remove("N_BILL_ID");

            #endregion 导出一

            filePath = ToExecl(BANK, ACCOUNT, dt, "票据台账明细", "银行票据台账");

            Response.Redirect(filePath);
        }

        protected void BindPint()
        {
            DataTable temp = (DataTable)ViewState["database"];
            string InerHtmls = "";

            var bank = "";
            if (txtDRAWERBank.Value != "")
            {
                bank = txtDRAWERBank.Value;
            }

            if (temp != null && temp.Rows.Count > 0)
            {
                InerHtmls += "<table border='1' style='border-collapse:collapse;'>";
                InerHtmls += @"<tr style='height: 40px; mso-height-source: userset; mso-height-alt: 600;'>
                            <th align='center' class='xl70' height='40' width='1945' colspan='18' style='height: 30.00pt;
                                width: 1458.75pt;border: .5pt solid windowtext;' x:str=''>
                                银行票据台账
                            </th>
                            </tr>";
                InerHtmls += @"<tr height='40' style='height: 30.00pt; mso-height-source: userset; mso-height-alt: 600;'><td colspan='18' style='border: .5pt solid windowtext;'><td></tr>";

                InerHtmls += @"<tr height='40' class='xl65' style='height: 30.00pt; mso-height-source: userset;mso-height-alt: 600;'>
                            <td class='xl74' colspan='18' style='border: .5pt solid windowtext;' x:str=''>
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                开户行：" + bank + @"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                账号：" + txtDRAWERACCOUNT.Value.Trim() + @"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            </td>
                        </tr>";

                InerHtmls += @" <tr height='40' class='xl66' style='height: 30.00pt; mso-height-source: userset;mso-height-alt: 600;'>
                            <td class='xl75' height='120' rowspan='3' style='width: 16px; height: 90.00pt; border: .5pt solid windowtext; ' x:str=''>
                                序号
                            </td>
                            <td class='xl75' rowspan='3' style='width: 100px; border: .5pt solid windowtext;' x:str=''>
                                票据种类
                            </td>
                            <td class='xl75' colspan='3' style='border: .5pt solid windowtext;'x:str=''>
                                购入情况
                            </td>
                            <td class='xl75' colspan='12' style='border: .5pt solid windowtext;'x:str=''>
                                使用情况
                            </td>
                            <td class='xl75' rowspan='3' style='width: 100px; border: .5pt solid windowtext;' x:str=''>
                                承兑时间
                            </td>
                        </tr>";

                InerHtmls += @"<tr height='40' class='xl66' style='height: 30.00pt; mso-height-source: userset;mso-height-alt: 600;'>
                            <td class='xl75' rowspan='2' style='width: 100px; border: .5pt solid windowtext;' x:str=''>
                                日&nbsp;&nbsp;&nbsp;&nbsp;期
                            </td>
                            <td class='xl75' rowspan='2' style='width: 100px; border: .5pt solid windowtext;' x:str=''>
                                票据号码
                            </td>
                            <td class='xl75' rowspan='2' style='width: 15px; border: .5pt solid windowtext;' x:str=''>
                                份数
                            </td>
                            <td class='xl75' rowspan='2' style='width: 100px; border: .5pt solid windowtext;' x:str=''>
                                出票日期
                            </td>
                            <td class='xl76' rowspan='2' style='width: 100px; border: .5pt solid windowtext;' x:str=''>
                                票据期限（月）
                            </td>
                            <td class='xl75' rowspan='2' style='width: 100px; border: .5pt solid windowtext;' x:str=''>
                                到期日
                            </td>
                            <td class='xl75' rowspan='2' style='width: 100px; border: .5pt solid windowtext;' x:str=''>
                                票据号码
                            </td>
                            <td class='xl75' rowspan='2' style='width: 100px; border: .5pt solid windowtext;' x:str=''>
                                收款单位
                            </td>
                            <td class='xl75' colspan='2' style='border: .5pt solid windowtext;'x:str=''>
                                用&nbsp;&nbsp;&nbsp;&nbsp;途
                            </td>
                            <td class='xl81' rowspan='2' style='width: 100px; border: .5pt solid windowtext;' x:str=''>
                                金额(元）
                            </td>
                            <td class='xl82' rowspan='2' style='width: 100px; border: .5pt solid windowtext;' x:str=''>
                                经办人
                            </td>
                            <td class='xl82' rowspan='2' style='width: 100px; border: .5pt solid windowtext;' x:str=''>
                                收票时间
                            </td>
                            <td class='xl82' rowspan='2' style='width: 100px; border: .5pt solid windowtext;' x:str=''>
                                收票人签字
                            </td>
                            <td class='xl75' rowspan='2' style='width: 100px; border: .5pt solid windowtext;' x:str=''>
                                收票人联系电话
                            </td>
                        </tr>";

                InerHtmls += @"<tr height='40' class='xl66' style='height: 30.00pt; mso-height-source: userset;mso-height-alt: 600;'>
                            <td class='xl75' style='width: 100px;'>
                                项目名称
                            </td>
                            <td class='xl75' style='width: 100px;'>
                                合同编号
                            </td>
                        </tr>";
                for (int i = 0; i < temp.Rows.Count; i++)
                {
                    InerHtmls += "<tr height='40' style='height: 30.00pt; mso-height-source: userset; mso-height-alt: 600;'>";
                    InerHtmls += "<td class='xl77' style='width:15px;'>" + temp.Rows[i]["R"].ToString() + "</td>";
                    InerHtmls += "<td class='xl77' style='width:100px;'>" + temp.Rows[i]["N_TYPE"].ToString() + "</td>";
                    InerHtmls += "<td class='xl78' style='width:100px;'>" + temp.Rows[i]["D_STARTDATE"].DateFormat("yyyy-MM-dd") + "</td>";
                    InerHtmls += "<td class='xl78' style='width:100px;'>" + temp.Rows[i]["C_BILLCODE"].ToString() + "</td>";
                    InerHtmls += "<td class='xl77' style='width:15px;'>" + temp.Rows[i]["N_SUM"].ToString() + "</td>";
                    InerHtmls += "<td class='xl78' style='width:100px;'>" + temp.Rows[i]["D_STARTDATE"].DateFormat("yyyy-MM-dd") + "</td>";
                    InerHtmls += "<td class='xl79' style='width:100px;'>" + temp.Rows[i]["N_TERMOFBILL"].ToString() + "</td>";
                    InerHtmls += "<td class='xl78' style='width:100px;'>" + temp.Rows[i]["D_ENDDATE"].DateFormat("yyyy-MM-dd") + "</td>";
                    InerHtmls += "<td class='xl78' style='width:100px;'>" + temp.Rows[i]["N_TERMOFBILL"].ToString() + "</td>";
                    InerHtmls += "<td class='xl78' style='width:100px;'>" + temp.Rows[i]["C_PAYEECMYNAME"].ToString() + "</td>";
                    InerHtmls += "<td class='xl78' style='width:100px;'>" + temp.Rows[i]["C_PROJECTNAME"].ToString() + "</td>";
                    InerHtmls += "<td class='xl78' style='width:100px;'>" + temp.Rows[i]["C_PROJECTCODE"].ToString() + "</td>";
                    InerHtmls += "<td class='xl83' style='width:100px;'>" + temp.Rows[i]["N_INVOICEVALUE"].ToString() + "</td>";
                    InerHtmls += "<td class='xl84' style='width:100px;'>" + temp.Rows[i]["C_DRAWERCMYNAME"].ToString() + "</td>";
                    InerHtmls += "<td class='xl78' style='width:100px;'>" + temp.Rows[i]["D_COLLECTTICKETSTIME"].DateFormat("yyyy-MM-dd") + "</td>";
                    InerHtmls += "<td class='xl84' style='width:100px;'>" + temp.Rows[i]["C_PAYEECMYNAME"].ToString() + "</td>";
                    InerHtmls += "<td class='xl77' style='width:100px;'>" + temp.Rows[i]["C_PAYEENUMBER"].ToString() + "</td>";
                    InerHtmls += "<td class='xl77' style='width:100px;'>" + temp.Rows[i]["D_ACCEPTANCETIME"].DateFormat("yyyy-MM-dd") + "</td>";
                    InerHtmls += "</tr>";
                }
                InerHtmls += "</table>";
            }
            this.Div1.InnerHtml = InerHtmls;
        }

        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void PRINT_CLICK(object sender, EventArgs e)
        {
            var key = Guid.NewGuid().ToString();
            Session[key] = Div1.InnerHtml;
            MessageBox.ResponseScript(Page, "top.MessageBox.Print({Value:\"ID=HTML&VALUE=" + key + "\",Area:[\"900px\",\"700px\"],Scrollbar:true})");
        }
    }
}